package com.itshops.dao.impl;

import com.itshops.dao.CartDao;
import com.itshops.entity.Cart;
import com.itshops.entity.Product;
import com.itshops.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartDapImpl implements CartDao {

    @Override
    public Cart hasCart(Integer uId, String pId) throws SQLException, InvocationTargetException, IllegalAccessException {
        //查询购物车是否存在,同时查询商品,多表查询
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_rice as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ? and c.p_id = ?;";
        //将数据封装到一个map集合
        Map<String, Object> query = runner.query(sql, new MapHandler(), uId, pId);
        if (query == null) {
            return null;
        }
        Cart cart = new Cart();
        Product product = new Product();
        //将数据映射到对应的实体中
        BeanUtils.populate(cart,query);
        BeanUtils.populate(product,query);
        //再将商品信息传给购物车
        cart.setProduct(product);
        return cart;
    }

    @Override
    public void updateCart(Cart cart) throws SQLException {
        //修改购物车里的数据,购物车数量和小计
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update cart set c_num = ? , c_count = ? where c_id = ?";
        runner.update(sql, cart.getCnum(),cart.getCcount(),cart.getCid());
    }

    @Override
    public void insertCart(Cart cart) throws SQLException {
        //添加购物车
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "insert into cart (u_id,p_id,c_num,c_count) value(?,?,?,?)";
        runner.update(sql,cart.getUid(),cart.getPid(),cart.getCnum(),cart.getCcount());

    }

    @Override
    public List<Cart> selectCartsByUid(Integer uId) throws SQLException, InvocationTargetException, IllegalAccessException {
        //查询cart需要关联商品表
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_rice as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ?;";
        List<Map<String, Object>> query = runner.query(sql, new MapListHandler(), uId);
        if (query==null){
            return null;
        }
        List<Cart> carts=new ArrayList<Cart>();
        for (Map<String, Object> map : query) {
            //cart+product
            Cart cart = new Cart();
            Product product = new Product();
            BeanUtils.populate(cart,map);
            BeanUtils.populate(product,map);
            cart.setProduct(product);
            carts.add(cart);
        }
        return carts;
    }

    @Override
    public void deleteCartByCid(String cid) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

        String sql = "delete from cart where c_id = ?;";

        queryRunner.update(sql, cid);
    }

    @Override
    public void updateByCid(BigDecimal count, BigDecimal bigDecimal, String cid) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update cart set c_count = ? , c_num = ? where c_id = ?;";
        queryRunner.update(sql, count,bigDecimal,cid);
    }

    @Override
    public void deleteCarByUid(String uid) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "delete from cart where u_id = ?;";
        queryRunner.update(sql, uid);
    }


}
